﻿/*---------------------------------------------------------------------*/
// fileName="DNMapping.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using SureKAM.BackUp.Notify.Domain;

namespace SureKAM.BackUp.Notify.DataAccess.implement
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public  class DNMappingRes:IDNMapping
    {
        #region 私有字段
        private List<Domain.DirectoryNotifyMapping> directoryNotifyMappings;
        private XMLHelper xmlHelper;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造方法
        /// </summary>
        public  DNMappingRes()
        {
            this.xmlHelper = new XMLHelper(AppDomain.CurrentDomain.BaseDirectory + "ConfigFile\\DirectoryNotifyMaping.xml");
            this.directoryNotifyMappings = this.xmlHelper.GetNodes<Domain.DirectoryNotifyMapping>();
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 添加一个DN对象
        /// </summary>
        /// <param name="directoryNotifyMapping"></param>
        /// <returns></returns>
        public string InsertDN(Domain.DirectoryNotifyMapping directoryNotifyMapping)
        {
           this.directoryNotifyMappings.Add(directoryNotifyMapping);
            return directoryNotifyMapping.Id;
        }

        /// <summary>
        /// 删除一个DN对象
        /// </summary>
        /// <param name="directoryNotifyMapping"></param>
        /// <returns></returns>
        public string DeleteDN(Domain.DirectoryNotifyMapping directoryNotifyMapping)
        {
            int i = 0;
            for(;i<this.directoryNotifyMappings.Count;i++)
            {
                if(this.directoryNotifyMappings[i].Id==directoryNotifyMapping.Id)
                {
                    break;
                }
            }
            if (i != this.directoryNotifyMappings.Count)
                this.directoryNotifyMappings.Remove(this.directoryNotifyMappings[i]);
            return directoryNotifyMapping.Id;
        }

        /// <summary>
        /// 根据ID查询DN对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Domain.DirectoryNotifyMapping GetDNById(string id)
        {
            var DNResult = from directoryNotifyMapping in directoryNotifyMappings
                           where directoryNotifyMapping.Id == id
                           select directoryNotifyMapping;
            return DNResult != null ? DNResult.FirstOrDefault() : null;
        }

        /// <summary>
        /// 获取所有的DN对象
        /// </summary>
        /// <returns></returns>
        public List<Domain.DirectoryNotifyMapping> GetDNs()
        {
            return this.directoryNotifyMappings;
        }

        /// <summary>
        /// 保存到文件
        /// </summary>
        /// <returns></returns>
        public bool SaveToFile()
        {
            return this.xmlHelper.SaveToFile<Domain.DirectoryNotifyMapping>(this.directoryNotifyMappings);
        }
        #endregion   
    }
}
